package com.tal.bcc.pad.cloud.thread;


import com.tal.bcc.pad.cloud.Log;
import com.tal.bcc.pad.cloud.entity.CloudResult;
import com.tal.bcc.pad.cloud.entity.CloudUploadEntity;
import com.tal.bcc.pad.cloud.listener.CloudTaskListener;
import com.tal.bcc.pad.cloud.listener.StsUploadListener;
import com.tal.bcc.pad.cloud.utils.CloudUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

public class CloudUpload {
    List<CloudResult> listUpload = new ArrayList();
    StsUploadListener uploadListener;
    Hashtable<Integer, Integer> progressMap = new Hashtable();
    int progress = 0;
    Hashtable<Integer, CloudUploadEntity> listResult = new Hashtable();
    String taskId;
    int fileSize = 0;
    boolean isComplete = false;
    CloudResult callBack;
    AtomicInteger taskStatus = new AtomicInteger(1);
    List<CloudUploadEntity> files;
    CloudTaskListener taskListener;
    int uploadFileSize;
    long systemTime;
    StsUploadListener xesStsUploadListener = new StsUploadListener() {
        public void onProgress(CloudResult result, int percent) {
            CloudUpload.this.progressMap.put(result.getTaskId(), percent);
            CloudUpload.this.getProcess(result);
        }

        public synchronized void onSuccess(CloudResult result) {
            if (CloudUpload.this.listUpload != null && CloudUpload.this.listUpload.size() > 0) {
                CloudUpload.this.listUpload.remove(0);
            }

            CloudUpload.this.executeUpload();
            CloudUploadEntity entity = new CloudUploadEntity();
            entity.setFileId(result.getFileId());
            entity.setHttpPath(result.getHttpPath());
            entity.setFilePath(result.getUploadFilePath());
            CloudUpload.this.listResult.put(result.getTaskId(), entity);
            if (CloudUpload.this.listResult.values().size() == CloudUpload.this.files.size()) {
                CloudResult xes = new CloudResult();
                xes.setHttpPathList(new ArrayList(CloudUpload.this.listResult.values()));
                CloudUpload.this.uploadListener.onProgress(result, 100);
                CloudUpload.this.isComplete = true;
                if (xes.getHttpPathList() != null && xes.getHttpPathList().size() > 0) {
                    xes.setHttpPath(((CloudUploadEntity)xes.getHttpPathList().get(0)).getHttpPath());
                }

                CloudUpload.this.uploadListener.onSuccess(xes);
                CloudUpload.this.taskComplete();
            } else {
                CloudUpload.this.listResult.size();
                CloudUpload.this.files.size();
            }

        }

        public synchronized void onError(CloudResult result) {
            if (CloudUpload.this.taskStatus.get() != 3) {
                CloudUpload.this.taskComplete();
                CloudUpload.this.uploadListener.onError(result);
            }

        }
    };

    public CloudUpload( List<CloudUploadEntity> files, StsUploadListener uploadListener, CloudTaskListener taskListener) {
        this.uploadListener = uploadListener;
        this.taskListener = taskListener;
        this.setTaskId();
        this.files = files;
        this.callBack = new CloudResult();
        taskListener.onReady();
    }

    public void setTaskId() {
        this.taskId = UUID.randomUUID().toString();
    }

    public void asyncUpload() {
        this.taskStatus.set(2);
        if (this.files != null && this.files.size() != 0) {
            this.fileSize = this.files.size();

            for(int i = 0; i < this.files.size(); ++i) {
                if ("".equals(((CloudUploadEntity)this.files.get(i)).getFilePath())) {
                    this.taskComplete();
                    this.callBack.setErrorMsg("上传文件地址不能为空");
                    this.uploadListener.onError(this.callBack);
                    return;
                }

                if (((CloudUploadEntity)this.files.get(i)).getCloudPath() == null) {
                    this.taskComplete();
                    this.callBack.setErrorMsg("上传文件云地址不能为空");
                    this.uploadListener.onError(this.callBack);
                    return;
                }

                if (!this.checkFile(((CloudUploadEntity)this.files.get(i)).getFilePath())) {
                    this.taskComplete();
                    this.callBack.setErrorMsg("请确认" + ((CloudUploadEntity)this.files.get(i)).getFilePath() + "文件是否存在");
                    this.uploadListener.onError(this.callBack);
                    return;
                }

                CloudResult cloudResult = new CloudResult();
                String cloudUri = CloudUtil.getOssFileUri(((CloudUploadEntity)this.files.get(i)).getFilePath(), ((CloudUploadEntity)this.files.get(i)).getCloudPath());
                cloudResult.setFileId(cloudUri);
                cloudResult.setCloudDir(((CloudUploadEntity)this.files.get(i)).getCloudPath().getName());
                cloudResult.setCloudFilePath((((CloudUploadEntity)this.files.get(i)).isLoadOSSBackupHost() ? "http://upload.saasz.vdyoo.com" : "http://upload.xueersi.com") + cloudUri);
                cloudResult.setCloudOSSHost(((CloudUploadEntity)this.files.get(i)).isLoadOSSBackupHost() ? "upload.saasz.vdyoo.com" : "upload.xueersi.com");
                cloudResult.setUploadFilePath(((CloudUploadEntity)this.files.get(i)).getFilePath());
                cloudResult.setUploadFileUri(cloudUri);
                cloudResult.setFileType(((CloudUploadEntity)this.files.get(i)).getType());
                cloudResult.setUploadListener(this.uploadListener);
                cloudResult.setTaskId(i + 1);
                cloudResult.setHttpPath("https://static0.xesimg.com" + cloudUri.replace("/v3/", "/"));
                this.listUpload.add(cloudResult);
            }

            if (this.listUpload != null && this.listUpload.size() != 0) {
                this.uploadFileSize = this.listUpload.size();
                if (this.listResult.values().size() < this.files.size()) {
                    this.executeUpload();
                }

            } else {
                CloudResult xes = new CloudResult();
                xes.setHttpPathList(new ArrayList(this.listResult.values()));
                this.uploadListener.onProgress((CloudResult)null, 100);
                this.isComplete = true;
                this.uploadListener.onSuccess(xes);
                this.taskComplete();
            }
        } else {
            this.taskComplete();
            this.callBack.setErrorMsg("上传内容不能为空");
            this.uploadListener.onError(this.callBack);
        }
    }

    private void executeUpload() {
        if (this.listUpload != null && this.listUpload.size() > 0) {
            try {
                ThreadPoolManager.getInstance().execute(new UploadTask((CloudResult)this.listUpload.get(0), this.xesStsUploadListener));
            } catch (Exception var3) {
                Exception e = var3;
                if (this.uploadListener != null) {
                    this.taskStatus.set(3);
                    CloudResult result = new CloudResult();
                    result.setErrorCode("2");
                    result.setErrorMsg(Log.getStackTraceString(e));
                    this.uploadListener.onError(result);
                    this.taskListener.onComplete();
                }
            }
        }

    }

    private void getProcess(CloudResult result) {
        long perTime = System.currentTimeMillis() - this.systemTime;
        if (perTime >= 500L) {
            int process = 0;

            Integer in;
            for(Iterator var5 = this.progressMap.values().iterator(); var5.hasNext(); process += in / this.uploadFileSize) {
                in = (Integer)var5.next();
            }

            if (this.progress < process && !this.isComplete) {
                this.progress = process;
                this.uploadListener.onProgress(result, this.progress);
            }

            this.systemTime = System.currentTimeMillis();
        }
    }

    public int getTaskStatus() {
        return this.taskStatus.get();
    }

    private void taskComplete() {
        this.taskStatus.set(3);
        this.taskListener.onComplete();
    }

    private boolean checkFile(String filePath) {
        try {
            File f = new File(filePath);
            if (!f.exists()) {
                return false;
            } else {
                return f.length() > 0L;
            }
        } catch (Exception var3) {
            return false;
        }
    }

}
